/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*!
  \file gpsParser.cxx
  \brief class implemenation for gpsParser

  \author Jack Elston
  $Date: 2005/02/12 00:20:45 $
*/

#include <unistd.h>
#include <signal.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

#include "aodvParser.h"
#include "color.h"

/*<-------Defines--------->*/
/*<------End Defines------>*/

/*<---Global Variables---->*/
extern bool verbose;
/*<-End Global Variables-->*/

AODVParser::AODVParser(String name) : ThreadedPipe(name)
{
}

void AODVParser::update() 
{
	int val, maxFD;
	fd_set readFDs, writeFDs;                   // fd sets
	struct timeval timeout;

	FD_ZERO(&readFDs);	                    		// zero read fd set
	FD_ZERO(&writeFDs);	                    		// zero write fd set
	FD_SET(fifo_FDs[INBOARD_RX],&readFDs);			// add fifo
	FD_SET(fifo_FDs[OUTBOARD_RX],&readFDs);			// add fifo
	FD_SET(fifo_FDs[INBOARD_TX],&writeFDs);			// add fifo
	FD_SET(fifo_FDs[OUTBOARD_TX],&writeFDs);		// add fifo

	maxFD = fifo_FDs[INBOARD_RX];
	if(fifo_FDs[OUTBOARD_RX] > maxFD) maxFD = fifo_FDs[OUTBOARD_RX];
	if(fifo_FDs[INBOARD_TX] > maxFD) maxFD = fifo_FDs[INBOARD_TX];
	if(fifo_FDs[OUTBOARD_TX] > maxFD) maxFD = fifo_FDs[OUTBOARD_TX];

	timeout.tv_sec = TIME_OUT_SEC; timeout.tv_usec = TIME_OUT_USEC;

	if( (val = select(maxFD + 1, &readFDs, &writeFDs,NULL,&timeout)) <= 0) {
		if( val != 0 ) {
			if (errno != EINTR) {
				perror("AODVParser::update() select");
				execRate = -1;
			} else
				cout << getName() << "::update() - select interrupted" << endl;
		}
	}
	else {
		if(FD_ISSET(fifo_FDs[OUTBOARD_RX], &readFDs))
		{
			int n = 0;
			n=readFifo(OUTBOARD_RX);
			if(n>0) {
				if (AODVUnpack()) {
					if(FD_ISSET(fifo_FDs[INBOARD_TX], &writeFDs))
						writeFifo(INBOARD_TX);
					else cout << "INBOARD_TX NOT READY" << endl;
				}
			}
			else cout << "BAD READ on OUTBOARD_RX" << endl;
		}
		if(FD_ISSET(fifo_FDs[INBOARD_RX], &readFDs)) {
			int n = 0;
			n=readFifo(INBOARD_RX);
			if(n>0) {
				if (AODVPack()) {
					if(FD_ISSET(fifo_FDs[OUTBOARD_TX], &writeFDs))
						writeFifo(OUTBOARD_TX);
					else cout << "OUTBOARD_TX NOT READY" << endl;
				}
			}
			else cout << "BAD READ on INBOARD_RX" << endl;
		}
	}
}

bool AODVParser::AODVPack (void) {
	if(fifo_msg.size + AODV_HDR_SIZE > FIFO_BUF_SIZE) {
		perror("AODVParser::AODVPack - Buffer not large enough to accomodate packet");
		return false;
	}

	// make some room
	for(int i = fifo_msg.size-1; i >= 0; i--)
		fifo_msg.data[i+AODV_HDR_SIZE] = fifo_msg.data[i];

	// add new information
	((uint32_t *)fifo_msg.data)[0] = fifo_msg.src.s_addr;
	((uint32_t *)fifo_msg.data)[1] = fifo_msg.dest.s_addr;

	fifo_msg.size = fifo_msg.size + AODV_HDR_SIZE;

	return true;
}

bool AODVParser::AODVUnpack (void) {

	Client * me = shmPtr->clients.getClient(0);

	fifo_msg.src.s_addr = ((uint32_t *)fifo_msg.data)[0]; //FIXME -- temporary fix
	fifo_msg.dest.s_addr  = shmPtr->clients.getAddr(me).s_addr;
	//fifo_msg.dest.s_addr = ((uint32_t *)fifo_msg.data)[1];
	fifo_msg.port = 1000 + (((uint32_t *)fifo_msg.data)[0] & 0xFF);

	for(int i = 0; i < (fifo_msg.size-AODV_HDR_SIZE); i++)
		fifo_msg.data[i] = fifo_msg.data[i+AODV_HDR_SIZE];

	fifo_msg.size = fifo_msg.size - AODV_HDR_SIZE;

	return true;
}
